# -*- coding:utf-8 -*-
__autor__ = 'wangjvv'
'''
description: 获取接口文档中定义的接口数据，执行接口请求
'''

from util.handleExcel import openExcel
from util.handleRequest import request
from util.handleLog import do_logger
from util.handleAssert import Assert  #自定义断言
from util.handleDependCase import dependCase
import os


class RunCase():

    def __init__(self):
        '''执行测试前的准备工作'''

    # def removeResultFile(self):
    #     '''删除已有的测试结果文件'''
    #     if resultFileName in os.listdir(testCasePath):
    #         os.remove(os.path.join(testCasePath,resultFileName))
    #     else:
    #         pass

    def runCase(self,sheetName,*caseId):
        '''
        执行单个或多个测试用例
        '''



    def runSheetCase(self,sheetName):
        '''
        执行单个页签的测试用例：使用循环的方式执行用例
        '''
        do_logger.info('开始测试%s模块接口'%sheetName)

        for row in range(2,openExcel.rows(sheetName)+1):  #循环便利页签中的所有行
            caseId = openExcel.caseId(sheetName,row)
            name = openExcel.name(sheetName, row)
            method = openExcel.method(sheetName,row) #获取Excel中每行的请求方法
            url = openExcel.url(sheetName,row)
            param = openExcel.request_param(sheetName,row)
            header = openExcel.header(sheetName,row)
            dependCaseId = openExcel.caseRely(sheetName,row)
            paramRely = openExcel.paramRely(sheetName,row) #获取请求参数依赖字段

            except_result = openExcel.except_result(sheetName,row)
            do_logger.info('开始测试：%s->%s 接口'%(caseId,name))
            flag = openExcel.excute_flag(sheetName,row) #获取执行标签  yes为执行 no为不执行
            if flag == 'yes':  #判断是否执行
                if caseId != '': #判断是否有用例依赖
                    dependData = dependCase.getDependData(sheetName,dependCaseId) #获取依赖的数据
                    param[paramRely] = dependData  #将依赖的数据加入到请求参数中
                    if  method== 'get':
                        response = request.get(url=url,params=param,headers=header)  #获取接口返回  get
                        result_info = Assert.assertData(except_result, response)  # 获取测试结果信息
                        if result_info[0] == 'PASS':  # 判断返回结果是否与预期结果一致
                            openExcel.write_result(openExcel.get_sheet(sheetName), row, result_info)
                        else:
                            openExcel.write_result(openExcel.get_sheet(sheetName), row, result_info)

                    #请求方法为post时
                    elif method == 'post':
                        response = request.post(url=url,data=param,headers=header)  #获取接口返回 post
                        result_info = Assert.assertData(except_result, response) #获取测试结果信息
                        if result_info[0] == 'PASS':  # 判断返回结果是否与预期结果一致
                            openExcel.write_result(openExcel.get_sheet(sheetName), row, result_info)
                        else:
                            openExcel.write_result(openExcel.get_sheet(sheetName), row, result_info)
                    else:
                        do_logger.error('暂不支持该请求方法：%s'%method)
                else:
                    if method == 'get':
                        response = request.get(url=url, params=param, headers=header)  # 获取接口返回  get
                        result_info = Assert.assertData(except_result, response)  # 获取测试结果信息
                        if result_info[0] == 'PASS':  # 判断返回结果是否与预期结果一致
                            openExcel.write_result(openExcel.get_sheet(sheetName), row, result_info)
                        else:
                            openExcel.write_result(openExcel.get_sheet(sheetName), row, result_info)

                    # 请求方法为post时
                    elif method == 'post':
                        response = request.post(url=url, data=param, headers=header)  # 获取接口返回 post
                        result_info = Assert.assertData(except_result, response)  # 获取测试结果信息
                        if result_info[0] == 'PASS':  # 判断返回结果是否与预期结果一致
                            openExcel.write_result(openExcel.get_sheet(sheetName), row, result_info)
                        else:
                            openExcel.write_result(openExcel.get_sheet(sheetName), row, result_info)
                    else:
                        do_logger.error('暂不支持该请求方法：%s' % method)
            else:
                pass

            do_logger.info('结束测试：%s->%s 接口'%(caseId,name))
        do_logger.info('结束测试%s模块接口'%sheetName)
        openExcel.save_result_file()

    def runSheetsCase(self,*sheetNames):
        '''
        执行多个页签的测试用例，传入多个页签名，参数传递后会自动保存为元祖型
        '''
        if isinstance(sheetNames,tuple):
            for sheetName in sheetNames:  #遍历元祖，循环执行
                self.runSheetCase(sheetName)
            openExcel.save_result_file()
        else:
            raise TypeError


    def runAllCase(self):
        '''
        执行所有测试用例
        '''
        allSheets = openExcel.get_all_sheetName()  #获取所有页签名
        for sheetName in allSheets:
            self.runSheetCase(sheetName)
        openExcel.save_result_file()


run = RunCase()

if __name__ == '__main__':
    # run.remove_resultFile()
    run.runSheetCase('customerQuery')
